home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / dfunclib.lha / dfunc_library / includes / Startup.asm < prev    next >
Assembly Source File  |  1995-11-01  |  12KB  |  458 lines

  1. *****************************************************************************
  2. *    "Startup.asm"
  3. *
  4. *    $VER: Startup_asm 3.5 (1.11.95)
  5. *
  6. *    Copyright © 1995 by Kenneth C. Nilsen/Digital Surface
  7. *    This source is freely distributable.
  8. *
  9. *    For instructions read the Startup_Asm.doc or the Startup_example.s
  10. *
  11. *SUMMARY:
  12. *
  13. *StartSkip    =    0 or 1 (0 = wb/cli, 1=cli only (eg. from AsmOne))
  14. *Processor    =    0 or 680x0
  15. *MathProc    =    0 or 6888x/68040/68060
  16. *
  17. *Init:    Version        <"PrgName ver.rev (date)">        (opt)
  18. *    TaskName    <"new task name in quotes">        (opt)
  19. *    TaskPri        <priority>                (opt)
  20. *    DefLib        <libname wo/.library>,<version>        (opt)
  21. *    DefEnd        ;must end the "Init:" section ALWAYS!    (required!)
  22. *
  23. *Start:    LibBase        <libname wo/.library>            (opt)
  24. *    StartFrom    = 0 (CLI) or <>0 (WB) in D0        (opt)
  25. *    TaskPointer    = pointer to taskstructure  (D0)    (opt)
  26. *    NextArg        = pointer to next argument or 0  (D0)    (opt)
  27. *    Return        <return value> (with RTS)        (recommended)
  28. *
  29. *****************************************************************************
  30.  
  31. zyxMax    = 17    ;max no. of libraries for AllocMem()
  32. zyxBufZ    = 308    ;size of format buffer in bytes for AllocMem()
  33.  
  34. ; Default settings will require 512 bytes allocated memory.
  35. ; The buffer is independent of this header.
  36.  
  37. *-----------------------------------------------------------------------------*
  38. * Macros for Startup.asm
  39. *-----------------------------------------------------------------------------*
  40.  
  41. Return:    Macro
  42.     moveq    #\1,d0    ;* if you use rtn code >127, change to "move.l"
  43.     rts                ;exit our program
  44.     EndM
  45.  
  46. DefLib:    Macro
  47.  
  48.     lea    \1NamX(pc),a1        ;make exclusive library namelabel
  49.     move.l    a1,(a5)+        ;put name into our buffer (if print)
  50.     move.l    a1,zyxNx        ;store name in local label
  51.     moveq    #\2,d0        ;if you use ver >127, change this to "move.l"
  52.     move.l    d0,(a5)+        ;store version in our buffer
  53.     move.l    d0,zyxVx        ;store version in local label
  54.     jsr    -552(a6)        ;try open library
  55.     move.l    d0,(a5)+        ;store result in our buffer
  56.  
  57.     move.l    d0,\1basX        ;store result in global label
  58.     bne.b    \1zyx            ;if <>0 then ok
  59.  
  60.     bsr.w    zyxLibR            ;print error message
  61.     bra.b    \1zyx            ;go on...
  62.  
  63. \1basX:    dc.l    0
  64. \1NamX:    dc.b    "\1.library",0        ;library name macro
  65.     even
  66. \1zyx:
  67.     EndM
  68.  
  69. DefEnd:    Macro
  70.     move.l    #-1,(a5)        ;this terminate our library list
  71.     rts
  72.     EndM
  73.  
  74. LibBase:    Macro
  75.     move.l    \1basX(pc),a6        ;get library base with exclusive name
  76.     EndM
  77.  
  78. TaskName:    Macro
  79.  
  80.     move.l    $4.w,a6            ;exec base
  81.     jsr    -132(a6)        ;Forbid()
  82.     move.l    zyxTask(pc),a0        ;get stored task pointer to our task
  83.     move.l    #.TaskN,10(a0)        ;get ptr. to new name and store
  84.     jsr    -138(a6)        ;Permit()
  85.     bra.b    .zyxTsk            ;go on...
  86.  
  87. .TaskN:    dc.b    \1            ;task name macro
  88.     dc.b    0            ;null terminate
  89.     even
  90. .zyxTsk:
  91.     EndM
  92.  
  93. TaskPri:    Macro
  94.     move.l    $4.w,a6            ;execbase
  95.     move.l    zyxTask(pc),a1        ;get stored task pointer
  96.     moveq    #\1,d0            ;get new task priority
  97.     jsr    -300(a6)        ;SetTaskPri()
  98.     EndM
  99.  
  100. TaskPointer:    Macro
  101.     move.l    zyxTask(pc),d0        ;give pointer to task in d0
  102.     EndM
  103.  
  104. StartFrom:    Macro
  105.     move.l    RtnMess(pc),d0        ;if started from WB, d0<>0
  106.     EndM
  107.  
  108.  
  109. NextArg:    Macro
  110.     move.l    zyxArgP(pc),d0        ;get address to argument string
  111.     beq.b    *+8            ;none? (from WB) then skip
  112.     move.l    d0,a0            ;use pointer
  113.     bsr.w    zyxGArg            ;go to our internal routine
  114.     move.l    a0,zyxArgP        ;update argument pointer
  115.     tst.l    d0            ;set/unset Z flag
  116.     EndM
  117.  
  118. ;*! Don't use the Version macro from Startup.asm 3.5+ !* :
  119.  
  120. Version:    Macro
  121.     bra.s    .zyxVer
  122.     dc.b    "$VER: "
  123.     dc.b    \1
  124.     dc.b    0
  125.     even
  126. .zyxVer:
  127.     EndM
  128.  
  129. *-----------------------------------------------------------------------------*
  130. * MAIN routine:
  131. *-----------------------------------------------------------------------------*
  132.  
  133. GoZYX:    move.l    a0,-(sp)        ;store argument pointer
  134.  
  135.     move.l    d0,zyxArgL        ;store length of arg. string
  136.     move.l    a0,zyxArgP        ;store arg. pointer in internal label
  137.  
  138.     move.l    $4.w,a6            ;exec base
  139.     move.l    a6,execbasX        ;allow "LibBase exec"
  140.  
  141.     move.l    #zyxBufZ,d0        ;set buffer size
  142.     moveq    #1,d1            ;requirements (public, clear)
  143.     jsr    -198(a6)        ;AllocMem()
  144.     move.l    d0,zyxBuff        ;store result in label
  145.     beq.w    .DOS            ;Null? then exit
  146.  
  147.     sub.l    a1,a1            ;a1=0 (this task)
  148.     jsr    -294(a6)        ;FindTask()
  149.     move.l    d0,a4            ;copy result
  150.     move.l    d0,zyxTask        ;store for internal use
  151.  
  152.     tst.l    172(a4)            ;where we started from (wb/cli)
  153.     bne.b    .chkPro            ;<>0 then cli
  154.  
  155.     moveq    #StartSkip,d0        ;check if we wanne skip (eg. AsmOne)
  156.     bne.b    .chkPro            ;yepp, then skip
  157.     lea    92(a4),a0        ;get message port address
  158.     jsr    -384(a6)        ;WaitPort()
  159.     lea    92(a4),a0        ;get message port address
  160.     jsr    -372(a6)        ;GetMsg()
  161.     move.l    d0,RtnMess        ;store message pointer in label
  162.  
  163. .chkPro:
  164.     move.w    296(a6),d5        ;AttnFlags in execbase
  165.  
  166.     move.l    #Processor,d7        ;processor we want
  167.     beq.w    .ProOk            ;null? then any will do, skip this part
  168.     cmp.w    #60,d7
  169.     ble.w    .nxPro1            ;then skip too...
  170.     sub.l    #68000,d7
  171.     beq.w    .ProOk
  172.  
  173. .nxPro1:
  174.     cmp.b    #10,d7            ;68010?
  175.     bne.b    .nxPro2            ;no, check next
  176.     and.b    #$cf,d5            ;check bits
  177.     bne.w    .ProOk            ;we got a 68010 or higher
  178.     bra.w    .ProErr            ;we got lower, we can't start...
  179.  
  180. .nxPro2:
  181.     cmp.b    #20,d7            ;same as above, just higher processor
  182.     bne.b    .nxPro3
  183.     and.b    #$ce,d5
  184.     bne.w    .ProOk
  185.     bra.b    .ProErr
  186.  
  187. .nxPro3:
  188.     cmp.b    #30,d7
  189.     bne.b    .nxPro4
  190.     and.b    #$cc,d5
  191.     bne.b    .ProOk
  192.     bra.b    .ProErr
  193.  
  194. .nxPro4:
  195.     cmp.b    #40,d7
  196.     bne.b    .nxPro5
  197.     and.b    #$c8,d5
  198.     bne.b    .ProOk
  199.     bra.b    .ProErr
  200.  
  201. .nxPro5:
  202.     cmp.b    #60,d7            ;we want a 68060 (yes, we do :) )
  203.     bne.b    .ProWho            ;not? then I dont know about any higher
  204.     btst    #7,d5            ;test if it is a 68060 we're using
  205.     beq.b    .ProErr            ;nope
  206.     btst    #6,d5            ;are you sure?
  207.     bne.b    .ProOk            ;yepp, continue
  208.     bra.b    .ProErr            ;not a 060, print error message
  209.  
  210. .ProWho:
  211.     lea    ProcWho(pc),a0        ;unknown processor required, print
  212.     move.l    #Processor,ProcNum    ;print message about it...
  213.     lea    ProcNum(pc),a1
  214.     bsr.w    zyxPrt
  215.     bra.w    .End
  216.  
  217. .ProErr:
  218.     st    zyxLR
  219.     lea    ProcErr(pc),a0        ;we don't got the processor required
  220.     add.l    #68000,d7
  221.     move.l    d7,ProcNum        ;message about it.
  222.     lea    ProcNum(pc),a1
  223.     bsr.w    zyxPrt            ;jump to our cli print routine
  224.  
  225. .ProOk:    move.l    #MathProc,d7        ;time to check for math-co-processor
  226.     beq.w    .MathOk            ;null? then any will do...
  227.     sub.l    #68000,d7
  228.  
  229.     cmp.w    #881,d7            ;a 68881?
  230.     bne.b    .Math2            ;no check next
  231.     and.b    #$70,d5            ;check flags
  232.     bne.b    .MathOk            ;we got it
  233.     bra.b    .MathEr            ;sorry...
  234.  
  235. .Math2:    cmp.w    #882,d7            ;same as above
  236.     bne.b    .Math3
  237.     and.b    #$60,d5
  238.     bne.b    .MathOk
  239.     bra.b    .MathEr
  240.  
  241. .Math3:    cmp.b    #60,d7
  242.     beq.b    .m60ok
  243.     cmp.b    #40,d7            ;we have 040/060 with FPU not 881/882
  244.     bne.b    .MathEr            ;unknown FPU if any else...
  245. .m60ok    btst    #6,d5            ;we got it?
  246.     bne.b    .MathOk            ;yepp, continue
  247.  
  248. .MathEr:
  249.     st    zyxLR
  250.     lea    ProcErr(pc),a0        ;print error message...
  251.     move.l    #MathProc,ProcNum    ;number data
  252.     lea    ProcNum(pc),a1
  253.     bsr.w    zyxPrt
  254.  
  255. .MathOk:
  256.     bsr.w    zyxLibO            ;oki, open our libraries
  257.  
  258.     tst.b    zyxLR            ;any errors?
  259.     bne.b    .noShow            ;yepp, don't start
  260.  
  261.     move.l    zyxArgP(pc),a0        ;get arg. pointer
  262.     move.l    zyxArgL(pc),d0        ;get arg. length
  263.  
  264.     bsr.w    Start            ;! start main program !
  265.     move.l    d0,zyxVal        ;store return code
  266.  
  267. .noShow:
  268.     bsr.w    zyxLibC            ;close libraries if any
  269.  
  270. .End:    move.l    zyxBuff(pc),d0        ;get pointer to our buffer
  271.     beq.b    .noBuff            ;no buffer?!?
  272.     move.l    d0,a1            ;copy pointer
  273.     move.l    #zyxBufZ,d0        ;length of buffer
  274.     jsr    -210(a6)        ;FreeMem()
  275.  
  276. .noBuff:
  277.     tst.l    RtnMess            ;from WB?
  278.     beq.w    .DOS            ;nope, from CLI
  279.  
  280.     jsr    -132(a6)        ;Forbid()
  281.     move.l    RtnMess(pc),a1        ;put message in a1
  282.     jsr    -138(a6)        ;Permit()
  283.  
  284. .DOS:    movem.l    (sp)+,a0        ;restore stack, put arg. pointer back
  285.     move.l    zyxVal(pc),d0        ;set return code
  286.     rts                ;BYE! :)
  287.  
  288. zyxDo:    move.b    d0,(a3)+        ;for RawDoFmt(), process routine
  289.     rts
  290.  
  291. zyxPrt:    movem.l    d0-a6,-(sp)        ;store regs. on stack
  292.  
  293.     lea    zyxDo(pc),a2        ;process
  294.     move.l    zyxBuff(pc),a3        ;format buffer
  295.     jsr    -522(a6)        ;RawDoFmt()
  296.  
  297.     moveq    #0,d0            ;any version
  298.     lea    zyxDos(pc),a1        ;ptr. to dos.library name
  299.     jsr    -552(a6)        ;OpenLibrary()
  300.     tst.l    d0            ;failed?
  301.     beq.b    .exit            ;jepp, exit
  302.     move.l    d0,a6            ;use dosbase
  303.  
  304.     jsr    -60(a6)            ;Output()
  305.     move.l    d0,d1            ;copy, set Z, failed?
  306.     beq.b    .clDos            ;no output -> close dos.library
  307.  
  308.     move.l    zyxBuff(pc),d2        ;get ptr. to our buffer
  309.     move.l    d2,a0            ;copy pointer
  310.     moveq    #0,d3            ;clear D3 (length of buffer)
  311. .count:    addq    #1,d3            ;add 1 to length of line
  312.     tst.b    (a0)+            ;get one char
  313.     bne.b    .count            ;null? yepp, found end...
  314.  
  315.     subq    #1,d3            ;exclude last sign (null)
  316.  
  317.     jsr    -48(a6)            ;print buffer to output handler (CLI)
  318.  
  319. .clDos:    lea    (a6),a1            ;copy dosbase to a1
  320.     move.l    $4.w,a6            ;get exebase
  321.     jsr    -414(a6)        ;CloseLibrary()
  322.  
  323. .exit:    movem.l    (sp)+,d0-a6        ;restore stack
  324.     rts                ;return
  325.  
  326. zyxLibO:
  327.     move.l    #4*3*zyxMax,d0        ;library buffer size 12*zyxMax (192)
  328.     moveq    #1,d1            ;any mem
  329.     jsr    -198(a6)        ;AllocMem()
  330.     move.l    d0,zyxMem        ;store result
  331.     beq.b    .memErr            ;null? then error
  332.  
  333.     move.l    d0,a5            ;use buffer
  334.     bsr.w    Init            ;jump to init section (see macros)
  335.  
  336.     rts                ;done
  337.  
  338. .memErr:
  339.     lea    zyxFR(pc),a0        ;get format text
  340.     lea    zyxMeR(pc),a1        ;get input string
  341.  
  342.     bsr.w    zyxPrt            ;print message about low memory
  343.  
  344.     st    zyxLR            ;failed, don't start main program
  345.     rts                ;return
  346.  
  347. zyxLibC:
  348.     move.l    $4.w,a6            ;execbase
  349.  
  350.     move.l    zyxMem(pc),d0        ;library buffer
  351.     beq.w    .noLibs            ;null? then no libraries
  352.     move.l    d0,a5            ;use pointer
  353.  
  354. .loop:    cmp.l    #-1,(a5)        ;end?
  355.     beq.b    .clEnd            ;yepp, then done!
  356.     move.l    8(a5),d0        ;get library base
  357.     beq.b    .noCl            ;null? then this lib. failed to open
  358.     move.l    d0,a1            ;use base
  359.     jsr    -414(a6)        ;CloseLibrary()
  360. .noCl:    lea    12(a5),a5        ;get next library base
  361.     bra.b    .loop            ;continue
  362.  
  363. .clEnd:    move.l    zyxMem(pc),a1        ;get lib. buffer pointer
  364.     move.l    #4*3*zyxMax,d0        ;size
  365.     jsr    -210(a6)        ;FreeMem()
  366.  
  367. .noLibs:
  368.     rts
  369.  
  370. zyxLibR:
  371.     st    zyxLR            ;if any errors, set error flag
  372.  
  373.     lea    zyxLib(pc),a0        ;pointer to format text
  374.     lea    zyxNx(pc),a1        ;pointer to format data
  375.     bsr.w    zyxPrt            ;print library name
  376.  
  377.     rts                ;return
  378.  
  379. zyxGArg:
  380.     move.b    (a0)+,d0        ;get a char from arg. line
  381.     beq.w    .end            ;null? end of line
  382.     cmp.b    #10,d0            ;linefeed?
  383.     beq.w    .end            ;end of line
  384.     cmp.b    #9,d0            ;tab?
  385.     beq.b    zyxGArg            ;get another char
  386.     cmp.b    #32,d0            ;space?
  387.     beq.b    zyxGArg            ;get another char
  388.  
  389.     move.l    zyxBuff(pc),a1        ;our text buffer
  390.     lea    -1(a0),a0        ;go back one byte on arg. line
  391. .copy:    move.b    (a0)+,d0        ;copy char to d0
  392.     beq.b    .stop            ;null? then stop copy
  393.     cmp.b    #10,d0            ;linefeed?
  394.     beq.b    .stop            ;stop copy
  395.     cmp.b    #32,d0            ;space?
  396.     beq.b    .eol            ;then this arg. is done
  397. .cont:    cmp.b    #'*',d0            ;asterix?
  398.     beq.b    .chkSpc            ;check for special functions
  399.     cmp.b    #'"',d0            ;quote?
  400.     beq.b    .toggle            ;toggle copy mode
  401. .noChk:    move.b    d0,(a1)+        ;copy passed char to our buffer
  402. .cont2:    bra.b    .copy            ;continue copy
  403.  
  404. .chkSpc:
  405.     cmp.b    #'"',(a0)        ;a quote want to be used?
  406.     bne.b    .chk2            ;no, check for a linefeed then...
  407.     move.b    #'"',(a1)+        ;copy a quote to our buffer
  408.     lea    1(a0),a0        ;skip one byte (2(*")->1("))
  409.     bra.b    .copy            ;continue copy argument
  410. .chk2:    cmp.b    #'n',(a0)        ;a linefeed?
  411.     bne.b    .noChk            ;nope, skip special funcs.
  412.     move.b    #10,(a1)+        ;copy a linefeed to our buffer
  413.     lea    1(a0),a0        ;make 2 -> 1
  414.     bra.b    .copy            ;continue copy
  415.  
  416. .toggle:
  417.     tst.b    zyxQ            ;already toggled?
  418.     beq.b    .set            ;nope, then toggle
  419.     sf    zyxQ            ;re toggle
  420.     bra.b    .stop            ;end of argument
  421. .set:    st    zyxQ            ;toggle so we can use space in arg.
  422.     bra.b    .cont2            ;continue copying argument
  423.  
  424. .eol:    tst.b    zyxQ            ;end of line -> toggled?
  425.     bne.b    .cont            ;jepp, continue
  426.  
  427. .stop:    tst.b    zyxQ            ;toggled?
  428.     bne.b    .end            ;jepp,don't care about this arg (error)
  429.     clr.b    (a1)            ;terminate buffer
  430.     move.l    zyxBuff(pc),d0        ;pointer to extracted argument
  431.     rts                ;return to macro
  432.  
  433. .end:    moveq    #0,d0            ;no more args
  434.     rts                ;return to macro
  435.  
  436. RtnMess:    dc.l    0        ;pointer to WB message
  437. ProcNum:    dc.l    0        ;processor wanted
  438. execbasX:    dc.l    0        ;pointer to execbase
  439. zyxArgL:    dc.l    0        ;argument line length
  440. zyxArgP:    dc.l    0        ;pointer to argument string
  441. zyxVal:        dc.l    0        ;return code
  442. zyxMem:        dc.l    0        ;pointer to library buffer
  443. zyxNx:        dc.l    0        ;temp lib. name
  444. zyxVx:        dc.l    0        ;temp lib. version
  445. zyxTask:    dc.l    0        ;pointer to task structure
  446. zyxBuff:    dc.l    0        ;pointer to string buffer
  447. zyxMeR:        dc.l    zyxMemR        ;pointer to a format string
  448. zyxLR:        dc.b    0        ;error flag
  449. zyxQ:        dc.b    0        ;toggle flag for quotes
  450.  
  451. zyxDos:        dc.b    'dos.library',0
  452. zyxLib:        dc.b    "Can't open %s ver. %ld",10,0
  453. zyxMemR:    dc.b    'Low memory!',10,0
  454. zyxFR:        dc.b    '%s',0
  455. ProcWho:    dc.b    'YEAH! %ld ?',10,0
  456. ProcErr:    dc.b    'Need %ld or better!',10,0
  457.         even
  458.